package com.woniuxy.controller;

import com.woniuxy.config.ElasticSearchConfig;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ElasticController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @GetMapping("/test")
    public ElasticSearchConfig test(){
        return elasticsearchRestTemplate.get("SzsV9XkBj_90AmdtwprX",ElasticSearchConfig.class);
    }
    @PostMapping("/test")
    public void post(){
        ElasticSearchConfig config = new ElasticSearchConfig();
        config.setId("10000");
        config.setMovieName("nimasile");
        config.setTimelength("2000");
        elasticsearchRestTemplate.save(config);
    }
    @GetMapping("/search")
    public SearchHits<ElasticSearchConfig> search(){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        NativeSearchQuery build = builder.withQuery(new MatchQueryBuilder("movieName.one", "严厉")).build();
        SearchHits<ElasticSearchConfig> search = elasticsearchRestTemplate.search(build, ElasticSearchConfig.class);
        return search;
    }
}
